//
//  SHCashUnionPaymentController.m
//  JiFengDaRen
//
//  Created by SH_iOS on 2018/1/11.
//  Copyright © 2018年 SH_iOS. All rights reserved.
//

#import "HomeNoticeModel.h"
#import "SHUnionPayModel.h"
#import "SHNoticeView.h"
#import "SHCashUnionPayCell.h"
#import "PaymentAmountController.h"
#import "BaseToolViewController.h"
#import "SHCashUnionPaymentController.h"
#import "BKRefreshGifHeader.h"
#import "JCCashRecordController.h"

@interface SHCashUnionPaymentController ()<UITableViewDelegate,UITableViewDataSource,SHNoticeViewDelegate,SHCashUnionPayCellDelegate>
@property(nonatomic,strong)UITableView *cashUnionTableView;//银联支付列表
@property(nonatomic,strong)NSMutableArray *cashUnionTypeArray;//银联支付类型

@property(nonatomic,strong)SHNoticeView * noticeView;//通知跑马灯
@property(nonatomic,strong)HomeNoticeModel *noticeModel;//通知模型
@end

@implementation SHCashUnionPaymentController

static NSString * const cashUnionCellReuserId = @"cashUnionCellReuserId";
static NSString * const cashUnionHeaderReuserId = @"cashUnionHeaderReuserId";

//- (void)viewWillAppear:(BOOL)animated{
//
//    [super viewDidLoad];
//    [_noticeView beginPaoMadengAnimations];//重启跑马灯动画
//}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"银联支付";
    [self.view addSubview:self.cashUnionTableView];
//    [self requestNoticeData];
    [self getCashUnionPayInfo];
    
    [self refreshCashUnionTableView];
    
    [self initRightItem];
    
}

- (void)initRightItem{
    
    UIBarButtonItem * rightItem = [[UIBarButtonItem alloc] initWithTitle:@"记录" style:UIBarButtonItemStylePlain target:self action:@selector(rightItemAction)];
    
    self.navigationItem.rightBarButtonItem = rightItem;
    
}

- (void)rightItemAction{
    JCCashRecordController * recordVC = [[JCCashRecordController alloc] init];
    
    [self.navigationController pushViewController:recordVC animated:YES];
}

#pragma mark - 刷新
- (void)refreshCashUnionTableView{
    
    @weakify(self)
    self.cashUnionTableView.mj_header =  [BKRefreshGifHeader bkHeaderWithRefreshingBlock:^{
        
        @strongify(self)
     
        [self getCashUnionPayInfo];
        [self.cashUnionTableView.mj_header endRefreshing];
        
    }];
}

- (NSMutableArray *)cashUnionTypeArray{
    
    if (!_cashUnionTypeArray) {
        
        _cashUnionTypeArray = [NSMutableArray arrayWithCapacity:10];
    }
    return _cashUnionTypeArray;
}

#pragma mark - 主列表
- (UITableView *)cashUnionTableView{
    if (!_cashUnionTableView) {
        
        _cashUnionTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth,KScreenHeight) style:UITableViewStyleGrouped];
        _cashUnionTableView.estimatedSectionHeaderHeight = 0;
        _cashUnionTableView.estimatedSectionFooterHeight = 0;
        _cashUnionTableView.estimatedRowHeight = 0;
        _cashUnionTableView.dataSource = self;
        _cashUnionTableView.delegate = self;
        //银联支付一cell
        [_cashUnionTableView registerClass:[SHCashUnionPayCell class] forCellReuseIdentifier:cashUnionCellReuserId];
        [_cashUnionTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cashUnionHeaderReuserId];

        _cashUnionTableView.backgroundColor = [UIColor clearColor];
        _cashUnionTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
    }
    return _cashUnionTableView;
}



#pragma mark - UITableViewDataSource & UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section == 0) {
        return 1;
    }
    return self.cashUnionTypeArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        __I6Height(46);
    }
    return __I6Height(45);

    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        
        UITableViewCell *header = [tableView dequeueReusableCellWithIdentifier:cashUnionHeaderReuserId];
        header.backgroundColor = [UIColor colorWithHexString:@"#f85154"];
        header.selectionStyle  = UITableViewCellSelectionStyleNone;
        
        UIImageView *unionImgHeader = [[UIImageView alloc]init];
        unionImgHeader.image = [UIImage imageNamed:@"unionpay_header"];
        [header addSubview:unionImgHeader];
        [unionImgHeader mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.centerX.mas_equalTo(header.mas_centerX);
            make.centerY.mas_equalTo(header.mas_centerY);
            make.width.equalTo(@(__I6Width(198 * 0.5)));
            make.height.equalTo(@(__I6Width(64 * 0.5)));

        }];
        return header;
        
    }else{
        
        SHCashUnionPayCell *unionpayCell = [tableView dequeueReusableCellWithIdentifier:cashUnionCellReuserId forIndexPath:indexPath];
        SHUnionPayModel *unionPayModel = self.cashUnionTypeArray[indexPath.row];
        unionPayModel.index  = indexPath.row;
        unionpayCell.unionPayModel = unionPayModel;
        unionpayCell.delegate = self;
        return unionpayCell;
    }

}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0.01;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 1) {
        return __I6Height(12);
    }
    return 0.01;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 1) {
        
        UIView *header = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, __I6Height(12))];
        header.backgroundColor = KBASEBGCOLOR;
        return  header;
    }
    
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2* NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        
    });
    
    SHUnionPayModel *unionModel  = self.cashUnionTypeArray[indexPath.row];
    if (!unionModel.status) {
        
        [Tool alertWithMessage:[NSString stringWithFormat:@"%@",unionModel.maintenance]  title:unionModel.use_title  who:self];
      
        return;
    }
    PaymentAmountController *paymentAmountVc =  [[PaymentAmountController alloc]init];
    paymentAmountVc.title = @"银联支付";
    switch (unionModel.cash_type) {
        case 0:
            paymentAmountVc.cashPaymentType = SHCashPaymentTftPay;
            break;
           
        case 1:
            paymentAmountVc.cashPaymentType = SHCashPaymentHzfPay;
            break;
            
        default:
            break;
    }

    [self.navigationController pushViewController:paymentAmountVc animated:YES];

}

#pragma mark - 通知跑马灯代理
#pragma mark - 移除跑马灯代理
//
//- (void)removeNoticeView:(SHNoticeView *)notice{
//    
//    [_noticeView removeFromSuperview];
//    
//}
//#pragma mark -- 查看跑马灯详情
//- (void)lookNoticeDetail:(SHNoticeView *)notice{
//
//    BaseToolViewController * noticeDetail = [[BaseToolViewController alloc]init];
//    noticeDetail.title = _noticeModel.title;
//    noticeDetail.urlString = _noticeModel.url;
//    [self.navigationController pushViewController:noticeDetail animated:YES];
//}

#pragma mark - 弹出费率说明
- (void)cashUnionPayCell:(SHCashUnionPayCell *)cell  showRateTitle:(NSString *)rateTilte Msg:(NSString *)rateMsg{
    
    [Tool alertWithMessage:[NSString stringWithFormat:@"%@          ",rateMsg]  title:rateTilte  who:self];
    
}

#pragma mark - 请求通知数据
//- (void)requestNoticeData{
//
//    [SHHomeModelView shShowNoticeScreenType:@"03" success:^(HomeNoticeModel *noticeModel) {
//        _noticeModel = noticeModel;
//        if (noticeModel.url != nil) {
//            _noticeView = [[SHNoticeView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, __I6Height(36)) title:noticeModel.title];
//            _noticeView.delegate = self;
//            self.cashUnionTableView.tableHeaderView  = _noticeView;
//        }
//    }];
//
//}

#pragma mark - 请求银联支付收款方式信息
- (void)getCashUnionPayInfo{
    
    [self.cashUnionTypeArray removeAllObjects];
    [MyDataService requestNoRSAAFURL:KGET_CAHS_PAY_INFO httpMethod:@"GET" params:nil data:nil complection:^(id result) {
        SHLog(@"KGET_CAHS_PAY_INFO : %@",result);
        if ([result[@"success"] boolValue]) {
            self.cashUnionTypeArray = [SHUnionPayModel mj_objectArrayWithKeyValuesArray:result[@"data"]];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.cashUnionTableView reloadData];
            });
        }
        
    } failure:^(id erro) {
        
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
